import {Component, EventEmitter,ViewChild, Input, OnInit, Output} from '@angular/core';
import {Router} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import { InitService } from '../../public/serve/init/init.service';
import { appTemplateUrl } from './template.url';
import { template } from './template.data';
import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { Service } from '../../server/server';
import { HintsService } from '../../public/serve/hints/hints.service';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
import {MessageService} from 'primeng/api';
import { TemplatePathTableComponent } from './template-path-table/template-path-table.component';
declare var $:any;

@Component({
  selector: 'app-template-path',
  templateUrl: './template-path.component.html',
  styleUrls: ['./template-path.component.css']
})

export class TemplatePathComponent implements OnInit {

  @ViewChild("table") table : TemplatePathTableComponent;

  @Input() status: any  = '';
  @Input() searchData: any;

  right: any = {
    "systemTemplateTath": {}
  };

  constructor( 
    public initService: InitService,
    public removeTableService: RemoveTableService,
    public request: CudTableService,
    public server: Service,
    private hintsService: HintsService,
    private messageService:MessageService
  ){
    this.right = $.extend(this.right, server.getRight());
  }

  isActive = this.server.isActive;
  ngOnInit() {
    this.server.eventEmit.subscribe((value: any)=>{
      this.isActive = value;
    });
  }

  /**
   * 启用
   */
  _enable(){
    this._status(0);
  }

   /**
    * 禁用
    */
   _disable(){
    this._status(-1);
   }

 /**
   * 分页
   * @param pagination 
   */ 
  _pagination(pagination: Array<any>) {
    template.pagination.pageNumber = pagination[0].pageNumber;
    template.pagination.pageSize   = pagination[0].pageSize;
    template.pagination.naturalNum = (template.pagination.pageNumber - 1) * template.pagination.pageSize;
    this.table.getExportPathList();
  }

  //搜索
  search() {
    template.retrieval.queryCriteria = this.searchData;
    template.retrieval.status = this.status;
    this.table.getExportPathList();
  }
  //响应搜索
  changesearch() {
      this.search();
  }
  
  /**
   * 批量启用或禁用
   * @param val
   */
  private _status(val:any){

    this.removeTableService
    .confirmEnableDisable(template.retrieval.token,'ids',this.server.baseUrl + appTemplateUrl.updateExportPathStatus,'status',val)
    .then(res => {
      if(res.result_code == "0000"){
        this.table.getExportPathList();
      }
      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err =>{
      this.hintsService.messages('error','error');
      return;
    });
  }
}
